package com.beiing.tianshuai.tianshuai.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ScrollView;

/**
 * Created by Jinni on 2017/4/7.
 */

public class TranslucentScrollView extends ScrollView {
    private OnScrollChangedListener mOnScrollChangedListener;

    public TranslucentScrollView(Context context) {
        super(context);
    }

    public TranslucentScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TranslucentScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldL, int oldT) {
        super.onScrollChanged(l, t, oldL, oldT);
        if (mOnScrollChangedListener != null) {
            boolean isScrollToBottom = getScrollY() != 0 && getScrollY() + getHeight() - getPaddingTop() - getPaddingBottom() == getChildAt(0).getHeight();
            mOnScrollChangedListener.onScrollChanged(this, l, t, oldL, oldT, isScrollToBottom);
        }
    }

    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mOnScrollChangedListener = listener;
    }

    public interface OnScrollChangedListener {
        void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt, Boolean isBottom);
    }

    @Override
    protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
        //禁止scrollView内布局变化后自动滚动
        return 0;
    }
}
